import 'package:lib_module_manager/component_manager/component.dart';
import 'package:lib_module_manager/module_manager/page.dart';
import 'package:uuid/uuid.dart';

class ComponentManager {
  static final ComponentManager instance = ComponentManager._internal();

  late final Map<String, CreateComponent> _createComponentFuncs = {};

  ComponentManager._internal();

  void registerComponent(
      {required String type, required CreateComponent createComponent}) {
    _createComponentFuncs[type] = createComponent;
  }

  Component? getComponent(
      {required Page page,
      required String type,
      String? id,
      dynamic value}) {
    CreateComponent? createComponent = _createComponentFuncs[type];

    if (createComponent == null) {
      return null;
    }

    return createComponent(
        page: page, id: id ?? const Uuid().v4(), value: value);
  }
}
